<?php


namespace app\controller;


use app\BaseController;
use app\Models\Login\LoginModel;
use think\exception\HttpException;
use think\facade\View;

class Login extends BaseController
{
    /**
     * 用户注册界面
     * @return string
     */
    public function register(){
        return View::fetch('login/register');
    }

    public function do_register(){
        $captcha = $this->request->param('captcha'); //验证码
        $password = $this->request->param('password'); //密码
        $re_password = $this->request->param('re_password'); //重复密码
        $username = $this->request->param('username'); //用户名
        if(empty($captcha) ||empty($password) || empty($re_password) || empty($username)){
            throw new HttpException(400,"必要参数不能为空");
        }
//        //校验验证码是否正确
//        if(!captcha_check($captcha)){
//            throw new HttpException(400,"验证码不正确");
//        };
        //校验两次密码书否输入一致
        if($password != $re_password){
            throw new HttpException(400,"两次输入密码不一致");
        }
        //检查用户名是否已经被注册过
        if(LoginModel::where('username',$username)->exists()){
            throw new HttpException(400,"用户名已经存在");
        }

        try {
            $login = new LoginModel();
            $login->sex = $this->request->param('sex',0);//性别赋值，默认值保密
            $login->birthday = strtotime($this->request->param('birthday'));//生日转化为时间戳赋值
            $login->create_time = time();//注册时间为当前时间
            $login->user_status = 1;//用户状态 0-禁用  1-正常
            $login->username = $username;
            $login->password = md5($password.mt_rand(1,10000));//密码加密
            $login->nickname = $this->request->param('nickname');//用户昵称
            $login->save();//保存
        }catch(\Exception $exception){
            throw new HttpException(400,"注册失败");
        }

        return json("请求成功");
    }
}